package com.thinkgem.jeesite.modules.fuct.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.web.util.WebUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.fuct.utils.CodeFormat;
import com.thinkgem.jeesite.modules.fuct.utils.WebserviceUtil;

/**
 * 接口测试Controller
 * @author Dean
 * @version 2017-02-21
 */
@Controller
@RequestMapping(value = "${adminPath}/fuct/webServiceTest")
public class WebServiceTestController extends BaseController {

	@RequestMapping(value = "index")
	public String index(Model model) {
		return "modules/fuct/webServiceTest";
	}
	
	@RequiresPermissions("fuct:webservice:send")
	@RequestMapping(value = "send")
	public String send(HttpServletRequest request, HttpServletResponse response, Model model) {
		String address = WebUtils.getCleanParam(request,"address");
		String requireStr = WebUtils.getCleanParam(request,"requireStr");
		model.addAttribute("address",address);
		model.addAttribute("requireStr",requireStr);
		if(StringUtils.isEmpty(address)){
			addErrorMessage(model, "接口地址不能为空");
			return "modules/fuct/webServiceTest";
		}
		if(StringUtils.isEmpty(requireStr)){
			addErrorMessage(model, "请求报文不能为空");
			return "modules/fuct/webServiceTest";
		}
		String responseStr = null;
		try {
			responseStr = WebserviceUtil.sendHttpPost(requireStr, address);
			responseStr = CodeFormat.xmlFormat(responseStr);
		} catch (Exception e) {
			addErrorMessage(model, e.getMessage());
			return "modules/fuct/webServiceTest";
		}
		model.addAttribute("responseStr",responseStr);
		return "modules/fuct/webServiceTest";
	}
	
}
